//
//  WeekViewController.m
//  LeagueOfLegend
//
//  Created by appying_wxj on 14-7-4.
//  Copyright (c) 2014年 appying_wxj. All rights reserved.
//

#import "WeekViewController.h"
#import "Request.h"
#import "UIImageView+WebCache.h"
#import "MobClick.h"
@interface WeekViewController ()

@end

@implementation WeekViewController
@synthesize dictionary = _dictionary;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    // Do any additional setup after loading the view.
//    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, 320, 0.5)];
//    imageView.backgroundColor = [UIColor redColor];
//    [self.view addSubview:imageView];
    
    
    //创建控件
    [self creatHead];
    //创建请求
    [self  creatRequest];
    
    
    //显示时间的模块
    UILabel *timeLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 64.5, 320, 20)];
    NSDate * senddate = [NSDate date];
    NSCalendar * cal = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;
    NSDateComponents * component = [cal components:unitFlags fromDate:senddate];
    NSInteger year = [component year];
    NSInteger month = [component month];
    NSInteger day = [component day];
    //NSLog(@"%4d/%2d/%2d",year,month,day);
    NSString *times = [NSString stringWithFormat:@"%d年%d月%d日",year,month,day];
    timeLab.text = times;
    timeLab.textAlignment = NSTextAlignmentCenter;// 居中设置
    timeLab.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:timeLab];
    
    if (version>=7){
        timeLab.frame = CGRectMake(0, 64.5, self.view.frame.size.width, 20);
        
    }else{
        timeLab.frame = CGRectMake(0, 24.5, self.view.frame.size.width, 20);
    }
    
    
  
    
//    return;//抛弃下面的所有
    
    
}
- (void)viewWillAppear:(BOOL)animated {
//    [self getMyData];
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"WeekViewController"];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"WeekViewController"];
}
#pragma mark ------------
//创建控件
-(void)creatHead
{
    self.titleLab.text = @"本周免费英雄";
    if ([Request checkNet]) {
        //创建view
        _scfoForMyview = [[UIScrollView alloc] init];
        
        // _scfoForMyview.backgroundColor = [UIColor redColor];
        [self.view addSubview:_scfoForMyview];
        
    }
    else
    {
        [_scfoForMyview removeFromSuperview];
    }
    if (version>=7){
        _scfoForMyview.frame =  CGRectMake(0, 84.5, 320, self.view.frame.size.height-49);
    }else{
        _scfoForMyview.frame =  CGRectMake(0, 44.5, 320, self.view.frame.size.height-49);
    }
}
//请求数据
-(void)creatRequest
{
    NSString *reqStr = [NSString stringWithFormat:@"http://lol.data.shiwan.com/lolHeros/?filter=&type=free"];
    ASIFormDataRequest* request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:reqStr]];
    request.delegate = self;
    request.requestMethod = @"GET";
    request.tag = 1;
    [request startSynchronous];
}
#pragma mark -数据代理
- (void)requestFinished:(ASIHTTPRequest *)request
{
    SBJSON* json = [[SBJSON alloc]init];
    NSString* str = request.responseString;
    
    NSDictionary* dic =[ json objectWithString:str error:nil];
    
    if (dic == nil) {
        NSLog(@"请求失败  解析数据为空  失败tag : %d",request.tag);
        NSLog(@"尝试打印解析钱数据  %@",str);
    }
    if (request.tag == 1) {
        
        [self creatForView:_scfoForMyview data: [dic objectForKey:@"result"]];
    }
}
//创建数据
-(void)creatForView:(UIScrollView*)_scro data:(NSMutableArray*)_arr
{
    int hengPaiNumber = 4; //一横排需要展示的数量
    float wight = 0.0  ;
    float height = 0.0 ;
    float jiange = 0.0;//间隔
    for (int i = 0 ;i < [_arr count];i++) {
        NSDictionary* dic = [_arr objectAtIndex:i];
        
        JYYingYongView* ying = [[[NSBundle mainBundle] loadNibNamed:@"JYYingYongView" owner:nil options:nil] objectAtIndex:0];
        wight =  ying.frame.size.width;
        height =  ying.frame.size.height;
        jiange = (_scro.frame.size.width - ying.frame.size.width* hengPaiNumber)/(hengPaiNumber + 1);
        ying.frame = CGRectMake(jiange * (i%hengPaiNumber +1) + wight * (i%hengPaiNumber),jiange * (i /hengPaiNumber) + height * (i/hengPaiNumber),wight,height);
        [ying.imageForhead setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"img"]]];
        ying.labForName.text = [dic objectForKey:@"name_c"];
        [_scro addSubview:ying];
    }
    _scro.contentSize = CGSizeMake(_scro.frame.size.width, (height + jiange)*([_arr count]/hengPaiNumber>[_arr count]%hengPaiNumber?[_arr count]/hengPaiNumber + 1:[_arr count]/hengPaiNumber) + 100);
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSLog(@"请求超时  失败tag : %d",request.tag);
}
#pragma mark -数据代理 end

//#pragma mark--数据解析
//// 获取数据
//-(void)getMyData
//{
//    NSString *reqStr = [NSString stringWithFormat:@"http://lol.data.shiwan.com/lolHeros/?filter=&type=free"];
//    NSString *getStr = [Request requestData:reqStr];
//    //NSLog(@"______________%@",getStr);
//    self.dictionary = [Request JsonData:getStr];
//    
//    
//    
//    
//}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
